package com.briup.product_source.service.impl;


import com.briup.product_source.dao.ManagerBatchMapper;
import com.briup.product_source.dao.QuarantineRegistrationMapper;
import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.ManagerBatch;
import com.briup.product_source.pojo.QuarantineRegistration;
import com.briup.product_source.result.ResultCode;
import com.briup.product_source.service.QuarantineRegistrationService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuarantineRegistrationServiceImpl implements QuarantineRegistrationService {

    @Autowired
    private QuarantineRegistrationMapper quarantineRegistrationMapper;

    @Autowired
    private ManagerBatchMapper managerBatchMapper;


    @Transactional
    @Override
    public void saveOrUpdate(QuarantineRegistration qr) {
        /**
         * grBatchId 为空、为null或者只包含空白字符；
         * qr.getGrMechanism() 返回的字符串为空、为null或者只包含空白字符；
         * bQualified 为空、为null或者只包含空白字符；
         * qr.getGrImg() 返回的字符串为空、为null或者只包含空白字符；
         * qr.getGrTime() 返回的字符串为空、为null或者只包含空白字符。
         */
        String grBatchId = qr.getGrBatchId();
        String bQualified = qr.getBQualified();
        int count;

        if(!StringUtils.hasText(grBatchId)
                || !StringUtils.hasText(qr.getGrMechanism())
                || !StringUtils.hasText(bQualified)
                || !StringUtils.hasText(qr.getGrImg())
                || !StringUtils.hasText(qr.getGrTime())){
            throw new ServiceException(ResultCode.PARAM_IS_EMPTY);
        }

        if(qr.getGrId() == null){
            //id空,新增
            count = quarantineRegistrationMapper.insert(qr);
        }else {
            //id不为空，更新
            count = quarantineRegistrationMapper.updateByPk(qr);
        }
        if(count == 0){
            throw new ServiceException(ResultCode.FAIL);
        }

        // 修改批次表中对应批次的检疫状态和检疫合格状态
        if (managerBatchMapper.updateBQuarantineAndBQualified(bQualified, grBatchId) == 0) {
            throw new ServiceException(ResultCode.FAIL);
        }
    }

    @Override
    public PageInfo<QuarantineRegistration> queryAndPage(Integer pageNum, Integer pageSize, String grMechanism, String bQualified) {
        PageHelper.startPage(pageNum,pageSize);
        List<QuarantineRegistration> list = quarantineRegistrationMapper.selectAllUnquarantined(grMechanism,bQualified);
        PageInfo<QuarantineRegistration> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteByid(Integer id) {
        QuarantineRegistration qr = quarantineRegistrationMapper.selectByPk(id);
        if(qr == null){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }else {
            String batchId = qr.getGrBatchId();
            ManagerBatch mb = managerBatchMapper.selectById(batchId);
            if(mb.getBQuarantine().equals("已检疫")){
                if(quarantineRegistrationMapper.deleteById(id) == 0){
                    throw new ServiceException(ResultCode.FAIL);
                }
            }
        }
    }

    @Override
    public void deleteByIdAll(List<Integer> ids) {
        List<Integer> list = new ArrayList<>();
        int empty = 0;
        for(int i : ids){
            if(quarantineRegistrationMapper.selectByPk(i) == null){
                empty ++;
            }else{
                list.add(i);
            }
        }
        if(list.isEmpty()){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        if(empty == ids.size()){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        quarantineRegistrationMapper.deleteByChoice(list);
    }
}
